<?php

namespace App\Task;

use App\Model\CallBase;
use App\Model\CallHangup;
use App\Model\Message;
use App\Model\VoiceCall;
use App\Model\VideoCall;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 天天福利 - 正在呼叫的记录（切换主播）
 * @Crontab(name="ChangeHostForFreeTask", rule="*\/19 * * * * *", callback="execute", memo="检测新手福利通话",singleton=true, enable=true)
 */
class ChangeHostForFreeTask
{
    //超过秒数会切换主播
    protected $lmtDuration = 10;

    public function execute()
    {
        $this->runCheck(VideoCall::class);
        $this->runCheck(VoiceCall::class);
    }

    public function runCheck($callCls = null)
    {
        //获取当前新手福利正在拨叫的用户
        $callingLogs = $callCls::getCallsForDailyFree(CallBase::STATUS_NO_PICK);
        if (empty($callingLogs)) return;
        log_info(__CLASS__ . "==docheck==" . date('Y-m-d H:i:s') . "==Count:" . count($callingLogs));

        foreach ($callingLogs as $k => $log) {
            $duration = time() - $log['start_time'];
            //没有超时不处理
            if ($duration < $this->lmtDuration) continue;
            //挂断记录
            CallHangup::addRecord($log['chat_user_id'], $log['user_id'], null, $callCls::CALL_TYPE);
            //透传消息
            Message::sendSlient($log['user_id'], 'CHANGE_FOR_NEW');
        }
    }
}
